package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Label;
import com.battle.pojo.entity.LabelMp;
import com.battle.pojo.param.labelMp.*;
import com.battle.pojo.vo.label.GetLabelVo;
import com.battle.pojo.vo.labelMp.*;
import com.battle.pojo.dto.labelMp.*;
import com.battle.service.repository.in.LabelMpRepository;
import com.battle.service.repository.in.LabelRepository;
import com.battle.service.service.in.LabelMpService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;


import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：标签关联表服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class LabelMpServiceImpl implements LabelMpService {

    private final LabelMpRepository labelMpRepository;
    private final LabelRepository labelRepository;

    public LabelMpServiceImpl(LabelMpRepository labelMpRepository, LabelRepository labelRepository) {
        this.labelMpRepository = labelMpRepository;
        this.labelRepository = labelRepository;
    }


    /**
     * 创建标签关联表
     * @param ids
     * @param fid
     */
    @Override
    public void createLabelMp(List<Long> ids, Long fid) {
        //现根据fid查询
        LabelMp labelMp = new LabelMp();
        labelMp.setFid(fid);
        labelMp.setIsDelete(false);
        List<LabelMp> labelMpList = labelMpRepository.getListByEntity(labelMp);
        //如果有数据则删除
        if (CollectionUtil.isNotEmpty(labelMpList)){
            //提取labelMpList的id
            List<Long> deleteIds = labelMpList.stream().map(LabelMp::getId).distinct().collect(Collectors.toList());
            labelMpRepository.removeByIds(deleteIds);
        }

        //创建新的数据
        if (CollectionUtil.isNotEmpty(ids)){
            List<LabelMp> list = new ArrayList<>();
            for (Long id : ids) {
                LabelMp po = new LabelMp();
                po.setId(IdGenerator.SNOW.generate());
                po.setFid(fid);
                po.setLabelId(id);
                list.add(po);
            }
            labelMpRepository.saveBatch(list);
        }

    }

    /**
     * 获取标签关联表下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetLabelMpDownListVo> getLabelMpDownList(GetLabelMpDownListParam param) {
        LabelMp po = new LabelMp();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
            po.setIsDelete(false);
        }
        List<LabelMp> labelMpList = labelMpRepository.getListByEntity(po);

        List<GetLabelMpDownListVo> list = new ArrayList<>();
        for (LabelMp labelMp : labelMpList) {
            GetLabelMpDownListVo vo = new GetLabelMpDownListVo();
            BeanUtils.copyProperties(labelMp, vo);
            Label byId = labelRepository.getById(labelMp.getLabelId());
            if (byId!=null){
                vo.setName(byId.getName());
            }
            list.add(vo);
        }
        return list;
    }


    /**
     * 智能机器人-获取对应标签下的战例
     * @param param
     * @return
     */
    @Override
    public List<GetLabelMpDownListVo> getLabelByName(GetLabelMpListParam param) {
        List<String> textList=new ArrayList<>();

        IKSegmenter ikSegmenter = new IKSegmenter(new StringReader(param.getText()), true);
        Lexeme lexeme;
        try {
            while ((lexeme = ikSegmenter.next())!= null) {
                System.out.println(lexeme.getLexemeText());
                textList.add(lexeme.getLexemeText());
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        if (CollectionUtil.isNotEmpty(textList)){
            return labelMpRepository.getDownList(textList);
        }
        return Collections.EMPTY_LIST;
    }

    @Override
    public Map<Long, List<GetLabelVo>> getListByFidIds(List<Long> fidIds) {
        return labelMpRepository.getListByFidIds(fidIds);
    }

    @Override
    public Map<Long, List<GetLabelVo>> getListByLabelIds(List<Long> labelIds) {
        return labelMpRepository.getListByLabelIds(labelIds);
    }

    /**
     * 根据用户获取标签
     * @param fid
     * @return
     */
    @Override
    public List<LabelMp> getLabelByFid(Long fid) {
        return labelMpRepository.getBaseMapper().selectList(
                new LambdaQueryWrapper<LabelMp>()
                        .eq(LabelMp::getFid,fid)
                        .eq(LabelMp::getIsDelete,false)
        );
    }


}



